<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Spring,Java - SSM," />





  <link rel="alternate" href="/rss2.xml" title="老苏叶的小窝" type="application/atom+xml" />






<meta name="description" content="三层架构1、什么是三层架构？  在项目开发中，遵循的一种形式模式，分为三层 界面层：用啦接受客户端的输入，调用业务逻辑层进行功能处理，返回结果给客户端，过去的servlet就是界面层的功能 业务逻辑层：用来进行整个项目的业务逻辑处理，向上为界面层提供处理结果，向下问数据访问层要数据 数据访问层：专门用来进行数据库的增删查改操作，向上为业务逻辑层提供数据   各层之间的调用顺序是固定的，不允许跨">
<meta property="og:type" content="article">
<meta property="og:title" content="SSM框架之Mybatis框架的学习笔记">
<meta property="og:url" content="https://gitee.com/laosuye/laosuye.git/2024/06/20/SSM%E6%A1%86%E6%9E%B6%E4%B9%8BMybatis%E6%A1%86%E6%9E%B6%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="老苏叶的小窝">
<meta property="og:description" content="三层架构1、什么是三层架构？  在项目开发中，遵循的一种形式模式，分为三层 界面层：用啦接受客户端的输入，调用业务逻辑层进行功能处理，返回结果给客户端，过去的servlet就是界面层的功能 业务逻辑层：用来进行整个项目的业务逻辑处理，向上为界面层提供处理结果，向下问数据访问层要数据 数据访问层：专门用来进行数据库的增删查改操作，向上为业务逻辑层提供数据   各层之间的调用顺序是固定的，不允许跨">
<meta property="article:published_time" content="2024-06-20T00:52:59.000Z">
<meta property="article:modified_time" content="2024-06-20T01:38:28.000Z">
<meta property="article:author" content="老苏叶">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="Java - SSM">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"right","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/laosuye/laosuye.git/2024/06/20/SSM框架之Mybatis框架的学习笔记/"/>





  <title>SSM框架之Mybatis框架的学习笔记 | 老苏叶的小窝</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8f796a0dc919d18055bb061f82a6395";";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-right page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">老苏叶的小窝</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/laosuye/laosuye.git/2024/06/20/SSM%E6%A1%86%E6%9E%B6%E4%B9%8BMybatis%E6%A1%86%E6%9E%B6%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="老苏叶">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatra.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="老苏叶的小窝">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">SSM框架之Mybatis框架的学习笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2024-06-20T08:52:59+08:00">
                2024-06-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/Spring-SSM-Mybatis/" itemprop="url" rel="index">
                    <span itemprop="name">Spring - SSM - Mybatis</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2024/06/20/SSM%E6%A1%86%E6%9E%B6%E4%B9%8BMybatis%E6%A1%86%E6%9E%B6%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2024/06/20/SSM%E6%A1%86%E6%9E%B6%E4%B9%8BMybatis%E6%A1%86%E6%9E%B6%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i> 浏览
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  6.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长"> 分钟
                  29
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <meta name="referrer" content="no-referrer">

<h2 id="三层架构"><a href="#三层架构" class="headerlink" title="三层架构"></a>三层架构</h2><p>1、什么是三层架构？</p>
<ul>
<li>在项目开发中，遵循的一种形式模式，分为三层<ul>
<li>界面层：用啦接受客户端的输入，调用业务逻辑层进行功能处理，返回结果给客户端，过去的servlet就是界面层的功能</li>
<li>业务逻辑层：用来进行整个项目的业务逻辑处理，向上为界面层提供处理结果，向下问数据访问层要数据</li>
<li>数据访问层：专门用来进行数据库的增删查改操作，向上为业务逻辑层提供数据</li>
</ul>
</li>
<li>各层之间的调用顺序是固定的，不允许跨层访问<ul>
<li>界面层&lt;—-&gt;业务逻辑层&lt;—–&gt;数据访问层</li>
</ul>
</li>
</ul>
<h2 id="SSM框架"><a href="#SSM框架" class="headerlink" title="SSM框架"></a>SSM框架</h2><p>常用的况框架SSM</p>
<ul>
<li>Spring：它是整合其他框架的框架，它的核心是IOC和AOP，它由20多个模块构成的，在很多领域都提供了很好的解决方案，是一个很强大的存在</li>
<li>SpriingMVC：他是Spring家族的医院，专门用来优化控制器(Servlet)的，提供了极其简单数据提交，数据携带，页面跳转等功能</li>
<li>MyBatis：是持久化层的一个框架，用来进行数据库的访问哟话，专注于SQL语句，极大的简化了JDBC的访问</li>
</ul>
<p>什么是框架？</p>
<ul>
<li>它是一个半成品软件，将所有的公共的，重复的功能解决掉，帮助程序员快速高效的进行开发，他是可复用的，可扩展的</li>
</ul>
<h2 id="什么是MyBatis框架"><a href="#什么是MyBatis框架" class="headerlink" title="什么是MyBatis框架"></a>什么是MyBatis框架</h2><ul>
<li>是Apache的一个开源项目iBatis</li>
<li>MyBaits最主要用来完成数据访问层的优化，它专注于sql语句，简化了过去JDBC繁琐的访问机制</li>
<li>添加框架的步骤<ul>
<li>添加依赖</li>
<li>添加配置</li>
<li>创建ssm数据库，添加student表，并且插入数据</li>
</ul>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">create database ssm default charset utf8;</span><br><span class="line">use ssm;</span><br><span class="line">create table student(</span><br><span class="line">id int(11) AUTO_INCREMENT primary key,</span><br><span class="line">name varchar(255) default null,</span><br><span class="line">email varchar(255) default null,</span><br><span class="line">age int(11) default null</span><br><span class="line">) ENGINE&#x3D;InnoDB default charset&#x3D;utf8;</span><br><span class="line">insert into student(name,email,age) values(&quot;张三&quot;,&#39;zhangsan@126.com&#39;,22);</span><br><span class="line">insert into student(name,email,age) values(&quot;李四&quot;,&#39;lisi@126.com&#39;,21);</span><br><span class="line">insert into student(name,email,age) values(&quot;王五&quot;,&#39;wangwu@126.com&#39;,22);</span><br><span class="line">insert into student(name,email,age) values(&quot;赵六&quot;,&#39;zhaoliu@126.com&#39;,24);</span><br><span class="line">select * from student;</span><br></pre></td></tr></table></figure>

<p>配置SqlMapConfig.xml文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version&#x3D;&quot;1.0&quot; encoding&#x3D;&quot;utf-8&quot; ?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration PUBLIC &quot;-&#x2F;&#x2F;mybatis.org&#x2F;&#x2F;DTD Config 3.0&#x2F;EN&quot;</span><br><span class="line">        &quot;http:&#x2F;&#x2F;mybatis.org&#x2F;dtd&#x2F;mybatis-3-config.dtd&quot;&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">    &lt;!--读取属性文件，jdbc.properties</span><br><span class="line">    属性：resource：从resource目录下找执行名称的文件加载</span><br><span class="line">    url:使用绝对路径加载属性文件</span><br><span class="line">    --&gt;</span><br><span class="line">    &lt;properties resource&#x3D;&quot;jdbc.properties&quot;&gt;&lt;&#x2F;properties&gt;</span><br><span class="line">    &lt;!--配置数据库环境变量(数据库连接配置)--&gt;</span><br><span class="line">    &lt;!--environments里面可以配置多套environment</span><br><span class="line">        default：使用下面的environment标签的id属性进行执行配置</span><br><span class="line">    --&gt;</span><br><span class="line">    &lt;environments default&#x3D;&quot;development&quot;&gt;</span><br><span class="line">        &lt;!--id：就是提供给environment的default属性是有--&gt;</span><br><span class="line">        &lt;environment id&#x3D;&quot;development&quot;&gt;</span><br><span class="line">            &lt;!--配置事务管理器</span><br><span class="line">                type属性：指定事务管理的方式   </span><br><span class="line">                    JDBC:事务的控制交托给程序员处理</span><br><span class="line">                    MANAGED：由容器(Spring)来管理事务</span><br><span class="line">            --&gt;</span><br><span class="line">            &lt;transactionManager type&#x3D;&quot;JDBC&quot;&gt;&lt;&#x2F;transactionManager&gt;</span><br><span class="line">            &lt;!--配置数据源</span><br><span class="line">                type：指定不同的配置方式</span><br><span class="line">                    JNDI：Java命名目录接口，在服务器端进行数据库连接池的管理</span><br><span class="line">                    POOLED：使用数据库连接池进行数据库的连接配置</span><br><span class="line">                    UNPOLLED：不使用数据库连接池</span><br><span class="line">            --&gt;</span><br><span class="line">            &lt;dataSource type&#x3D;&quot;POOLED&quot;&gt;</span><br><span class="line">                &lt;!--配置数据库连接的基本参数--&gt;</span><br><span class="line">                &lt;property name&#x3D;&quot;driver&quot; value&#x3D;&quot;$&#123;jdbc.driverClassName&#125;&quot;&#x2F;&gt;</span><br><span class="line">                &lt;property name&#x3D;&quot;url&quot; value&#x3D;&quot;$&#123;jdbc.url&#125;&quot;&#x2F;&gt;</span><br><span class="line">                &lt;property name&#x3D;&quot;username&quot; value&#x3D;&quot;$&#123;jdbc.username&#125;&quot;&#x2F;&gt;</span><br><span class="line">                &lt;property name&#x3D;&quot;password&quot; value&#x3D;&quot;$&#123;jdbc.password&#125;&quot;&#x2F;&gt;</span><br><span class="line"></span><br><span class="line">            &lt;&#x2F;dataSource&gt;</span><br><span class="line">        &lt;&#x2F;environment&gt;</span><br><span class="line">    &lt;&#x2F;environments&gt;</span><br><span class="line">    &lt;!--注册mapper.xml文件</span><br><span class="line">        resource:从resource目录下找指定名称文件注册</span><br><span class="line">        url：使用绝对路径找文件注册</span><br><span class="line">        class：动态代理方式下的注册</span><br><span class="line">    --&gt;</span><br><span class="line">    &lt;mappers&gt;</span><br><span class="line">        &lt;mapper resource&#x3D;&quot;StudentMapper.xml&quot;&gt;&lt;&#x2F;mapper&gt;</span><br><span class="line">    &lt;&#x2F;mappers&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;configuration&gt;</span><br></pre></td></tr></table></figure>

<p>配置StudentMapper.xml文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version&#x3D;&quot;1.0&quot; encoding&#x3D;&quot;utf-8&quot; ?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-&#x2F;&#x2F;mybatis.org&#x2F;&#x2F;DTD Mapper 3.0&#x2F;EN&quot;</span><br><span class="line">        &quot;http:&#x2F;&#x2F;mybatis.org&#x2F;dtd&#x2F;mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;!--</span><br><span class="line">mapper:是整个文件的大标签，用来开始和结束xml</span><br><span class="line">属性：</span><br><span class="line">    namespace：指定命名空间(相当于包名)，用来区分不同mapper.xml文件中相同的id属性</span><br><span class="line">--&gt;</span><br><span class="line">&lt;mapper namespace&#x3D;&quot;su&quot;&gt;</span><br><span class="line">    &lt;!--</span><br><span class="line">    完成查询全部学生的功能</span><br><span class="line">    resultType:指定查询返回结果集的类型，如果是集合，则必须是泛型的类型</span><br><span class="line">    parameterType：如果有参数，则通过它来指定参数类型</span><br><span class="line">    --&gt;</span><br><span class="line">    &lt;select id&#x3D;&quot;getAll&quot; resultType&#x3D;&quot;com.bjpowernode.pojo.Student&quot;&gt;</span><br><span class="line">        select id,name,email,age</span><br><span class="line">        from student</span><br><span class="line">    &lt;&#x2F;select&gt;</span><br><span class="line">&lt;&#x2F;mapper&gt;</span><br></pre></td></tr></table></figure>

<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;使用文件流的读取核心配置文件SqlMapConfig</span><br><span class="line">InputStream in &#x3D; Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">&#x2F;&#x2F;创建SqlSessionFactory工厂</span><br><span class="line">SqlSessionFactory factory &#x3D; new SqlSessionFactoryBuilder().build(in);</span><br><span class="line">&#x2F;&#x2F;取出SqlSession的对象</span><br><span class="line">SqlSession sqlSession &#x3D; factory.openSession();</span><br><span class="line">&#x2F;&#x2F;完成查询操作</span><br><span class="line">List&lt;Student&gt; list &#x3D; sqlSession.selectList(&quot;su.getAll&quot;);</span><br><span class="line">list.forEach(student -&gt; System.out.println(student));</span><br><span class="line">&#x2F;&#x2F;关闭SqlSession</span><br><span class="line">sqlSession.close();</span><br></pre></td></tr></table></figure>

<p>使用mybatis进行增删查改</p>
<p>SqlMapConfig配置文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--按主键id查询学生信息--&gt;</span><br><span class="line">&lt;select id&#x3D;&quot;getById&quot; parameterType&#x3D;&quot;int&quot; resultType&#x3D;&quot;com.bjpowernode.pojo.Student&quot;&gt;</span><br><span class="line">    select id,name,email,age from student where id&#x3D;#&#123;id&#125;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--按学生名称模糊查询--&gt;</span><br><span class="line">&lt;select id&#x3D;&quot;getByName&quot; parameterType&#x3D;&quot;string&quot; resultType&#x3D;&quot;com.bjpowernode.pojo.Student&quot;&gt;</span><br><span class="line">    select id,name,email,age from student where name  like &#39;%$&#123;name&#125;%&#39;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--增加学生--&gt;</span><br><span class="line">&lt;insert id&#x3D;&quot;insert&quot; parameterType&#x3D;&quot;com.bjpowernode.pojo.Student&quot;&gt;</span><br><span class="line">    insert into student(name,email,age) values (#&#123;name&#125;,#&#123;email&#125;,#&#123;age&#125;)</span><br><span class="line">&lt;&#x2F;insert&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--按主键删除学生--&gt;</span><br><span class="line">&lt;delete id&#x3D;&quot;delete&quot; parameterType&#x3D;&quot;int&quot;&gt;</span><br><span class="line">    delete from student where id &#x3D; #&#123;id&#125;</span><br><span class="line">&lt;&#x2F;delete&gt;</span><br><span class="line"></span><br><span class="line">&lt;!--修改学生--&gt;</span><br><span class="line">&lt;update id&#x3D;&quot;update&quot; parameterType&#x3D;&quot;com.bjpowernode.pojo.Student&quot;&gt;</span><br><span class="line">    update student set name&#x3D;#&#123;name&#125;,email&#x3D;#&#123;email&#125;,age&#x3D;#&#123;age&#125; where id &#x3D; #&#123;id&#125;</span><br><span class="line">&lt;&#x2F;update&gt;</span><br></pre></td></tr></table></figure>

<p>测试程序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;读取核心配置文件</span><br><span class="line">InputStream in &#x3D; Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">&#x2F;&#x2F;创建SqlSessionFactory工厂对象</span><br><span class="line">SqlSessionFactory factory &#x3D; new SqlSessionFactoryBuilder().build(in);</span><br><span class="line">&#x2F;&#x2F;取出SqlSession</span><br><span class="line">SqlSession sqlSession &#x3D; factory.openSession();</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;完成查询操作</span><br><span class="line">        List&lt;Student&gt; list &#x3D; sqlSession.selectList(&quot;su.getAll&quot;);</span><br><span class="line">        list.forEach(student -&gt; System.out.println(student));</span><br><span class="line">        &#x2F;*for (Student x : list)&#123;</span><br><span class="line">            System.out.println(x);</span><br><span class="line">        &#125;*&#x2F;</span><br><span class="line">        &#x2F;&#x2F;关闭SqlSession</span><br><span class="line">        sqlSession.close();</span><br><span class="line"> &#x2F;&#x2F;按主键查询学生</span><br><span class="line">        Student stu &#x3D; sqlSession.selectOne(&quot;su.getById&quot;,1);</span><br><span class="line">        System.out.println(stu);</span><br><span class="line">        &#x2F;&#x2F;关闭SqlSession</span><br><span class="line">        sqlSession.close();</span><br><span class="line">&#x2F;&#x2F;执行模糊查询</span><br><span class="line">        List&lt;Student&gt; list &#x3D; sqlSession.selectList(&quot;su.getByName&quot;,&quot;李&quot;);</span><br><span class="line">        for (Student x : list)&#123;</span><br><span class="line">            System.out.println(x);</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F;list.forEach(student -&gt; System.out.println(student));</span><br><span class="line">        &#x2F;&#x2F;关闭SqlSession</span><br><span class="line">        sqlSession.close();</span><br><span class="line">&#x2F;&#x2F;增加学生</span><br><span class="line">        int num &#x3D; sqlSession.insert(&quot;su.insert&quot;, new Student(&quot;苏叶&quot;, &quot;210469@qq.com&quot;, 21));</span><br><span class="line">        sqlSession.commit();</span><br><span class="line">        sqlSession.close();</span><br><span class="line">&#x2F;&#x2F;删除学生</span><br><span class="line">        int delete &#x3D; sqlSession.delete(&quot;su.delete&quot;, 5);</span><br><span class="line">        sqlSession.commit();</span><br><span class="line">        sqlSession.close();</span><br><span class="line">&#x2F;&#x2F;修改学生</span><br><span class="line">        int update &#x3D; sqlSession.update(&quot;su.update&quot;, new Student(2, &quot;王麻子&quot;, &quot;wm@126.com&quot;, 18));</span><br><span class="line">        sqlSession.commit();</span><br><span class="line">        sqlSession.close();</span><br></pre></td></tr></table></figure>

<p><strong>注意：执行增删改语句一定要提交事务</strong></p>
<h2 id="Mybatis对象分析"><a href="#Mybatis对象分析" class="headerlink" title="Mybatis对象分析"></a>Mybatis对象分析</h2><h3 id="1、Resources类"><a href="#1、Resources类" class="headerlink" title="1、Resources类"></a>1、Resources类</h3><ul>
<li>就是解析SqlMapConfig.xml文件，创建出相应的对象</li>
<li>InputStream in = Resources.getResourceAsStream(“SqlMapConfig.xml”);</li>
</ul>
<h3 id="2、SqlSessionFactoryBuilder接口"><a href="#2、SqlSessionFactoryBuilder接口" class="headerlink" title="2、SqlSessionFactoryBuilder接口"></a>2、SqlSessionFactoryBuilder接口</h3><ul>
<li>使用ctrl+h查看本接口的子接口及实现类</li>
<li>DefaultSqlSessionFactory是实现类</li>
<li>SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);</li>
</ul>
<p>3、SqlSession接口</p>
<ul>
<li>DefaultSqlSession实现类</li>
<li>SqlSession sqlSession = factory.openSession();</li>
</ul>
<h2 id="为实体类注册别名"><a href="#为实体类注册别名" class="headerlink" title="为实体类注册别名"></a>为实体类注册别名</h2><h3 id="1、单个注册"><a href="#1、单个注册" class="headerlink" title="1、单个注册"></a>1、单个注册</h3><p>在SqlMapConfig.xml文件中properties 标签后 environments 前面加入标签，注册之后就可以在Mapper.xml文件中的resultType属性或者parameterType属性中使用这个别名</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--注册实体类的别名--&gt;</span><br><span class="line">&lt;typeAliases&gt;</span><br><span class="line">    &lt;!--单个注册--&gt;</span><br><span class="line">    &lt;typeAlias type&#x3D;&quot;com.bjpowernode.pojo.Student&quot; alias&#x3D;&quot;student&quot;&gt;&lt;&#x2F;typeAlias&gt;</span><br><span class="line">&lt;&#x2F;typeAliases&gt;</span><br></pre></td></tr></table></figure>

<h3 id="2、批量注册"><a href="#2、批量注册" class="headerlink" title="2、批量注册"></a>2、批量注册</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--批量注册别名</span><br><span class="line">别名是类名的驼峰命名规范</span><br><span class="line">--&gt;</span><br><span class="line">&lt;package name&#x3D;&quot;com.bjpowernode.pojo&quot;&#x2F;&gt;</span><br></pre></td></tr></table></figure>

<p>批量把这个目录下的所有实体类都注册别名，别名是类名的驼峰命名规范</p>
<p>输出日志：<strong>固定的写法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--设置日志输出底层执行的代码--&gt;</span><br><span class="line">&lt;settings&gt;</span><br><span class="line">    &lt;setting name&#x3D;&quot;logImpl&quot; value&#x3D;&quot;STDOUT_LOGGING&quot;&#x2F;&gt;</span><br><span class="line">&lt;&#x2F;settings&gt;</span><br></pre></td></tr></table></figure>

<h2 id="动态代理"><a href="#动态代理" class="headerlink" title="动态代理"></a>动态代理</h2><h3 id="1、动态代理存在的意义"><a href="#1、动态代理存在的意义" class="headerlink" title="1、动态代理存在的意义"></a>1、动态代理存在的意义</h3><ul>
<li>在三层架构中，业务逻辑层需要通过接口访问数据访问层的功能，可以使用动态代理</li>
</ul>
<h3 id="2、动态代理的实现规范"><a href="#2、动态代理的实现规范" class="headerlink" title="2、动态代理的实现规范"></a>2、动态代理的实现规范</h3><ul>
<li>UserMapper.xml文件与UserMapper.java的接口必须在同一个目录。</li>
<li>UserMapper.xml文件与UserMapper.java接口的文件名必须一致，后缀可以不管</li>
<li>UserMapper.xml文件中的标签id的值与UserMapper.java的接口中方法名称完全一致</li>
<li>UserMapper.xml文件中的标签的parameterType属性值与UserMapper.java的接口中方法参数类型完全一致</li>
<li>UserMapper.xml文件中标签的resultType值与UserMapper.java的接口方法的返回值类型必须一致</li>
<li>UserMapper.xml文件中标签的namespace属性必须是接口的完全限定名称，例如com.bjpowernode.mapper.UserMapper</li>
<li>在SqlMapConfig.xml文件中注册mapper文件时，使用class=接口的完全限定名称，例如com.bjpowernode.mapper.UserMapper</li>
</ul>
<h3 id="3、动态代理的访问"><a href="#3、动态代理的访问" class="headerlink" title="3、动态代理的访问"></a>3、动态代理的访问</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">InputStream in &#x3D; Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">      SqlSessionFactory factory &#x3D; new SqlSessionFactoryBuilder().build(in);</span><br><span class="line">      sqlSession &#x3D; factory.openSession();</span><br><span class="line">      uMapper &#x3D; sqlSession.getMapper(UsersMapper.class);</span><br><span class="line">	&#x2F;&#x2F;直接调用方法</span><br><span class="line">      List&lt;Users&gt; list &#x3D; uMapper.getAll();</span><br><span class="line">      for(Users x : list)&#123;</span><br><span class="line">         System.out.println(x);</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#x2F;&#x2F;</span><br><span class="line">&#x2F;&#x2F;原来的方式</span><br><span class="line">   public void testGetAll() throws IOException &#123;</span><br><span class="line">        &#x2F;&#x2F;完成查询操作</span><br><span class="line">       &#x2F;&#x2F;只能通过namespace标注下id的值来调用方法，而且不同的语句使用不同的sqlsession方法</span><br><span class="line">        List&lt;Student&gt; list &#x3D; sqlSession.selectList(&quot;su.getAll&quot;);</span><br><span class="line">        list.forEach(student -&gt; System.out.println(student));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>使用动态代理的方式进行获取数据</p>
<h2 id="和-的区别"><a href="#和-的区别" class="headerlink" title="#{}和${}的区别"></a>#{}和${}的区别</h2><p><strong>#{}占位符</strong></p>
<ul>
<li>传参的时候大部分使用#{}传参，它的底层使用的是PreparedStatement对象，是安全的数据库访问，防止sql注入</li>
<li>#{}里面写什么东西，主要看parameterType参数的类型<ul>
<li>如果parameterType的类型是简单类型(8种基本(封装)+String)，则#{}里面随便写</li>
</ul>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;getById&quot; resultType&#x3D;&quot;users&quot; parameterType&#x3D;&quot;int&quot;&gt; &gt;入参类型是简单类型</span><br><span class="line">       select id,username,birthday,sex,address from users where id &#x3D; #&#123;id&#125; &gt;这个里面可以随便写</span><br><span class="line">   &lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>parameterType的类型如果是实体类型，则#{}里面只能是类中的成员变量的名称，并且区分大小写</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id&#x3D;&quot;add&quot; parameterType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">        insert into users(username,birthday,sex,address) values(#&#123;userName&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;)</span><br><span class="line">    &lt;&#x2F;insert&gt;</span><br></pre></td></tr></table></figure>

<p><strong>${}字符串拼接或字符串替换</strong></p>
<ul>
<li>字符串拼接，一般用于模糊查询中，建议少用，因为有sql注入的风险</li>
<li>也分为两种情况，同样看parameterType类型<ul>
<li>如果parameterType类型是简单类型，则${}里面随便写，但是分版本，如果是3.5.1及以下版本，只能写value</li>
</ul>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;getByLike&quot; parameterType&#x3D;&quot;string&quot; resultType&#x3D;&quot;users&quot;&gt;  &gt;传参是简单类型</span><br><span class="line">        select id,username,birthday,sex,address from users </span><br><span class="line">    	where username like &#39;%$&#123;userName&#125;%&#39;	&gt;$&#123;&#125;里面可以随便写</span><br><span class="line">    &lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>parameterType的类型如果是实体类型，则${}里面只能是类中的成员变量的名称（溴铵在已经很少用了）</li>
</ul>
<p>优化模糊查询，因为直接使用${}这个进行拼接会有sql注入危险，所以以后我们使用concat()函数来进行模糊查询</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;getByNameGood&quot; parameterType&#x3D;&quot;string&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">        select id,username,birthday,sex,address from users where username like concat(&#39;%&#39;,#&#123;name&#125;,&#39;%&#39;)</span><br><span class="line">    &lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<p>字符串替换使用${}</p>
<p>在有的时候我们需要进行模糊查询的时候，不仅仅是根据用户名，有的时候根据地址进行模糊查询，这个时候我们肯定是不能写两个不同的sql语句，这样太麻烦了，所以这个时候我们就要需要用到注解</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Users&gt; getByNameOrAddress(</span><br><span class="line">            @Param(&quot;columnName&quot;) String columnName,</span><br><span class="line">            @Param(&quot;columnValue&quot;) String columnValue); </span><br><span class="line">&lt;!----&gt;    </span><br><span class="line">    &lt;!--如果参数超过一个则parameterType不用写--&gt;</span><br><span class="line">    &lt;select id&#x3D;&quot;getByNameOrAddress&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">        select id,username,birthday,sex,address from users where</span><br><span class="line">        $&#123;columnName&#125; like concat(&#39;%&#39;,#&#123;columnValue&#125;,&#39;%&#39;)</span><br><span class="line">    &lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<p>然后进行测试，要查询哪个字段，就靠自己传参</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">   public  void  testGetByNameGoodOrAddress()&#123;</span><br><span class="line">      List&lt;Users&gt; list &#x3D; uMapper.getByNameOrAddress(&quot;address&quot;,&quot;南&quot;);</span><br><span class="line">      for (Users x:list)&#123;</span><br><span class="line">         System.out.println(x);</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h2 id="返回主键业务"><a href="#返回主键业务" class="headerlink" title="返回主键业务"></a>返回主键业务</h2><p>假如在有的时候，我们插入一行数据，但是主键是自增的，并且关联了其他表，且也要为其增加数据，这样我们还要先查询自增的主键是多少再进行增加另一个表的数据，这样很麻烦，所以我们可以在插入数据的时候返回主键</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id&#x3D;&quot;add&quot; parameterType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">   &lt;selectKey keyProperty&#x3D;&quot;id&quot; resultType&#x3D;&quot;int&quot; order&#x3D;&quot;AFTER&quot;&gt;</span><br><span class="line">       select last_insert_id()</span><br><span class="line">   &lt;&#x2F;selectKey&gt;</span><br><span class="line">       insert into users(username,birthday,sex,address) values(#&#123;userName&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;)</span><br><span class="line">   &lt;&#x2F;insert&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>selectKey标签的详解：<ul>
<li>keyProperty：users对象的哪个属性来返回主键值</li>
<li>resultType：返回主键的类型</li>
<li>order：在插入语句前还是后返回主键的值</li>
</ul>
</li>
</ul>
<h2 id="动态SQL"><a href="#动态SQL" class="headerlink" title="动态SQL"></a>动态SQL</h2><ul>
<li>什么是动态sql<ul>
<li>可以定义代码片段，可以进行逻辑判断，可以进行循环处理(批量处理)，使条件判断更简单</li>
</ul>
</li>
<li>：用来定义代码片段，可以将所有的列名，或者复杂的条件定义为代码片段，供使用时调用</li>
<li>：用来引用标签定义的代码片段</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">  &lt;!--定义代码片段--&gt;</span><br><span class="line">  &lt;sql id&#x3D;&quot;allColumns&quot;&gt;</span><br><span class="line">      id,username,birthday,sex,address</span><br><span class="line">  &lt;&#x2F;sql&gt;</span><br><span class="line">&lt;!--在sql语句中引用--&gt;</span><br><span class="line">  &lt;select id&#x3D;&quot;getAll&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">      select &lt;include refid&#x3D;&quot;allColumns&quot;&gt;&lt;&#x2F;include&gt; from users</span><br><span class="line">  &lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>进行条件判断</li>
<li>进行多条件拼接，在查询，删除，更新中使用<ul>
<li>强化select查询，使用if进行判断</li>
</ul>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;getByCondition&quot; parameterType&#x3D;&quot;users&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">    select &lt;include refid&#x3D;&quot;allColumns&quot;&gt;&lt;&#x2F;include&gt; from users</span><br><span class="line">    &lt;where&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;userName !&#x3D; null and userName !&#x3D;&#39;&#39;&quot;&gt;</span><br><span class="line">            and username like concat(&#39;%&#39;,#&#123;userName&#125;,&#39;%&#39;)</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;birthday !&#x3D; null&quot;&gt;</span><br><span class="line">            and birthday &#x3D; #&#123;birthday&#125;</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;sex !&#x3D; null and sex !&#x3D; &#39;&#39;&quot;&gt;</span><br><span class="line">            and sex &#x3D; #&#123;sex&#125;</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;address !&#x3D; null and address !&#x3D; &#39;&#39;&quot;&gt;</span><br><span class="line">            and address like concat(&#39;%&#39;,#&#123;address&#125;,&#39;%&#39;)</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">    &lt;&#x2F;where&gt;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void testGetByCondition()&#123;</span><br><span class="line">   Users users &#x3D; new Users();</span><br><span class="line">   users.setUserName(&quot;小&quot;);</span><br><span class="line">   List&lt;Users&gt; list &#x3D; uMapper.getByCondition(users);</span><br><span class="line">   for (Users x : list)&#123;</span><br><span class="line">      System.out.println(x);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>:有选择的进行更新处理，至少更新一列</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;update id&#x3D;&quot;updateBySet&quot; parameterType&#x3D;&quot;users&quot; &gt;</span><br><span class="line">    update users</span><br><span class="line">    &lt;set&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;userName !&#x3D; null and userName !&#x3D; &#39;&#39;&quot;&gt;</span><br><span class="line">            username &#x3D; #&#123;userName&#125;,</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;birthday !&#x3D; null&quot;&gt;</span><br><span class="line">            birthday &#x3D; #&#123;birthday&#125;,</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;sex !&#x3D; null and sex !&#x3D; &#39;&#39;&quot;&gt;</span><br><span class="line">            sex &#x3D; #&#123;sex&#125;,</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">        &lt;if test&#x3D;&quot;address !&#x3D; null and address !&#x3D; &#39;&#39;&quot;&gt;</span><br><span class="line">            address &#x3D; #&#123;address&#125;,</span><br><span class="line">        &lt;&#x2F;if&gt;</span><br><span class="line">    &lt;&#x2F;set&gt;</span><br><span class="line">        where id &#x3D; #&#123;id&#125;</span><br><span class="line">&lt;&#x2F;update&gt;</span><br></pre></td></tr></table></figure>

<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void testUpdateBySet()&#123;</span><br><span class="line">   Users users &#x3D; new Users();</span><br><span class="line">   users.setId(6);</span><br><span class="line">   users.setUserName(&quot;TheShy&quot;);</span><br><span class="line">   int i &#x3D; uMapper.updateBySet(users);</span><br><span class="line">   sqlSession.commit();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>:用来进行循环遍历，完成循环条件查询，批量删除，批量增减，批量更新</p>
<ul>
<li>collection：用来指定入参的类型，如果是List集合则为小写的list，Map为map，数组Array是array</li>
<li>item：每次循环遍历出来的值或者对象</li>
<li>separator：多个值或者多个对象之间的分隔符</li>
<li>open：整个循环前的前括号</li>
<li>close：整个循环的后括号</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;getByIds&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">    select &lt;include refid&#x3D;&quot;allColumns&quot;&gt;&lt;&#x2F;include&gt;</span><br><span class="line">    from users</span><br><span class="line">    where id in (</span><br><span class="line">    &lt;foreach collection&#x3D;&quot;array&quot; item&#x3D;&quot;id&quot; separator&#x3D;&quot;,&quot; open&#x3D;&quot;(&quot; close&#x3D;&quot;)&quot;&gt;</span><br><span class="line">        #&#123;id&#125;</span><br><span class="line">    &lt;&#x2F;foreach&gt;</span><br><span class="line">     )</span><br><span class="line">&lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<p>使用foreach实现批量删除</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;delete id&#x3D;&quot;deleteBatch&quot;&gt;</span><br><span class="line">    delete from users</span><br><span class="line">    where id in</span><br><span class="line">    &lt;foreach collection&#x3D;&quot;array&quot; item&#x3D;&quot;id&quot; separator&#x3D;&quot;,&quot; open&#x3D;&quot;(&quot; close&#x3D;&quot;)&quot;&gt;</span><br><span class="line">        #&#123;id&#125;</span><br><span class="line">    &lt;&#x2F;foreach&gt;</span><br><span class="line">&lt;&#x2F;delete&gt;</span><br></pre></td></tr></table></figure>

<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void testDeleteBatch()&#123;</span><br><span class="line">   Integer[] array &#x3D; &#123;4,7&#125;;</span><br><span class="line">   int i &#x3D; uMapper.deleteBatch(array);</span><br><span class="line">   sqlSession.commit();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注意：如果要使用批量更新，需要再properties配置文件中的url后加上 &amp;allowMultiQueries=true</strong></p>
<h3 id="指定参数位置"><a href="#指定参数位置" class="headerlink" title="指定参数位置"></a>指定参数位置</h3><p>如果入参是多个，可以通过指定参数位置进行传参，是实体类包含不住的条件,实体类只能封装住成员变量的条件，如果某个成员变量需要有区间范围之内的判断，或者两个值进行处理，则实体类包不住</p>
<ul>
<li>例如：查询指定日期范围内的用户信息</li>
<li>首先定义接口中的方法，因为是在一个区间之内，所以需要两个参数的方法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Users&gt; getByBirthday(Date beigin,Date end);</span><br></pre></td></tr></table></figure>

<ul>
<li>然后在Mapper.xml文件中编写sql语句</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id&#x3D;&quot;getByBirthday&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">    select &lt;include refid&#x3D;&quot;allColumns&quot;&gt;&lt;&#x2F;include&gt;</span><br><span class="line">    from users</span><br><span class="line">    where birthday between #&#123;arg0&#125; and #&#123;arg1&#125;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<p>在进行多个参数查询的时候，我们可以用 arg0 开始指定，从零开始，依次递增</p>
<ul>
<li>测试</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void testGetByBirthday() throws ParseException &#123;</span><br><span class="line">   Date begin &#x3D; sf.parse(&quot;2001-01-1&quot;);</span><br><span class="line">   Date end &#x3D; sf.parse(&quot;2003-01-1&quot;);</span><br><span class="line">   List&lt;Users&gt; byBirthday &#x3D; uMapper.getByBirthday(begin, end);</span><br><span class="line">   for (Users x : byBirthday)&#123;</span><br><span class="line">      System.out.println(x);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="入参为Map"><a href="#入参为Map" class="headerlink" title="入参为Map"></a>入参为Map</h3><ul>
<li>如果入参超过一个以上，使用map封装查询条件，更有语义，查询条件更明确</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Users&gt; getByMap(Map map);</span><br><span class="line">&lt;!--</span><br><span class="line">#&#123;birthdayBegin&#125;  为map集合的key</span><br><span class="line">#&#123;birthdayEnd&#125;     为map集合的value</span><br><span class="line">--&gt;</span><br><span class="line">&lt;select id&#x3D;&quot;getByMap&quot; resultType&#x3D;&quot;users&quot;&gt;</span><br><span class="line">    select &lt;include refid&#x3D;&quot;allColumns&quot;&gt;&lt;&#x2F;include&gt;</span><br><span class="line">    from users</span><br><span class="line">    where birthday between #&#123;birthdayBegin&#125; and #&#123;birthdayEnd&#125;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br><span class="line">@Test</span><br><span class="line">public void testGetByMap() throws ParseException &#123;</span><br><span class="line">   Date begin &#x3D; sf.parse(&quot;2001-01-1&quot;);</span><br><span class="line">   Date end &#x3D; sf.parse(&quot;2003-01-1&quot;);</span><br><span class="line">   Map map &#x3D; new HashMap&lt;&gt;();</span><br><span class="line">   map.put(&quot;birthdayBegin&quot;,begin);</span><br><span class="line">   map.put(&quot;birthdayEnd&quot;,end);</span><br><span class="line">   List&lt;Users&gt; byBirthday &#x3D; uMapper.getByMap(map);</span><br><span class="line">   for (Users x : byBirthday)&#123;</span><br><span class="line">      System.out.println(x);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="返回值是map"><a href="#返回值是map" class="headerlink" title="返回值是map"></a>返回值是map</h3><ul>
<li>如果返回的数据实体类无法包含，可以使用map返回多张表中若干的数据，然后后这些数据之间没有任何关系，就是Object类型，map的key就是列名或者别名</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;返回值是map(一行)</span><br><span class="line">Map getReturnMap(Integer id);</span><br><span class="line">&#x2F;&#x2F;返回值为多行map</span><br><span class="line">List&lt;Map&gt; getAllMap();</span><br><span class="line">&lt;!--返回值为一行map--&gt;</span><br><span class="line">&lt;select id&#x3D;&quot;getReturnMap&quot; parameterType&#x3D;&quot;int&quot; resultType&#x3D;&quot;map&quot;&gt;</span><br><span class="line">    select username,address from users</span><br><span class="line">    where id &#x3D; #&#123;id&#125;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br><span class="line">&lt;!--返回值为多行map--&gt;</span><br><span class="line">&lt;select id&#x3D;&quot;getAllMap&quot; resultType&#x3D;&quot;map&quot;&gt;</span><br><span class="line">    select username,address from users</span><br><span class="line">&lt;&#x2F;select&gt;</span><br><span class="line">&#x2F;&#x2F;返回值为一行map</span><br><span class="line">@Test</span><br><span class="line">public void testGetReturnMap()&#123;</span><br><span class="line">   Map returnMap &#x3D; uMapper.getReturnMap(2);</span><br><span class="line">   System.out.println(returnMap.get(&quot;username&quot;));</span><br><span class="line">   System.out.println(returnMap.get(&quot;address&quot;));</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;返回多行Map</span><br><span class="line">@Test</span><br><span class="line">public void testGetAllMap()&#123;</span><br><span class="line">   List&lt;Map&gt; allMap &#x3D; uMapper.getAllMap();</span><br><span class="line">   for (Map x : allMap)&#123;</span><br><span class="line">      System.out.println(x);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="resultMap的用法"><a href="#resultMap的用法" class="headerlink" title="resultMap的用法"></a>resultMap的用法</h2><ul>
<li>在有的时候实体类的属性和数据库表的字段不一样的情况下，可以使用resultMap进行关系映射</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">  &lt;!--实体类和表中的字段不一样的情况下，可以使用resultMap手工完成映射--&gt;</span><br><span class="line">  &lt;resultMap id&#x3D;&quot;bookmap&quot; type&#x3D;&quot;book&quot;&gt;</span><br><span class="line">      &lt;!--</span><br><span class="line">id是你在sql语句中设置的id type是你映射的类型</span><br><span class="line">--&gt;</span><br><span class="line">      &lt;!--主键绑定--&gt;</span><br><span class="line">      &lt;!--property为你实体类中的属性，column为你数据库中对应的字段名--&gt;</span><br><span class="line">      &lt;id property&#x3D;&quot;id&quot; column&#x3D;&quot;bookid&quot;&gt;&lt;&#x2F;id&gt;</span><br><span class="line">      &lt;!--非主键绑定--&gt;</span><br><span class="line">      &lt;result property&#x3D;&quot;name&quot; column&#x3D;&quot;bookname&quot;&gt;&lt;&#x2F;result&gt;</span><br><span class="line">  &lt;&#x2F;resultMap&gt;</span><br><span class="line">  &lt;select id&#x3D;&quot;getAllBook&quot; resultMap&#x3D;&quot;bookmap&quot;&gt; &lt;!--设置返回值为resultMap，并且设置名字，为你resultMap中的id--&gt;</span><br><span class="line">      select bookid,bookname from book</span><br><span class="line">  &lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<h2 id="表的关联关系"><a href="#表的关联关系" class="headerlink" title="表的关联关系"></a>表的关联关系</h2><p>关联关系是有方向的</p>
<ul>
<li>一对多关联：一个老师可以教多个学生，多个学生只有一个老师来教，站在老师方向，就是一对多关联</li>
<li>多对一关联：一个老师可以教多个学生，多个学生只有一个老师来教，站在学生的方向，这就是多对一的关系</li>
<li>一对一关联：一个老师辅导一个学生，一个学生只请教一个老师，学生和老师一对一</li>
<li>多对多关联：园区划线的车位和园区的任意一辆车，一个车位可以停任意一辆车，任意一辆车可以停在任意的一个车位。</li>
<li><strong>一对多的关联管理</strong><ul>
<li>客户和订单就是典型的一对多关联关系，一个客户名下可以有多个订单，客户表是一方，订单表是多方</li>
</ul>
</li>
<li>使用一对多的关联关系，可以按组查询客户的同时查询该客名下所有的订单<ul>
<li>把两个表创建实体类，客户表可以创建一个订单类型的属性</li>
</ul>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;客户表</span><br><span class="line">    private Integer id;</span><br><span class="line">    private String name;</span><br><span class="line">    private Integer age;</span><br><span class="line">    private List&lt;Orders&gt; ordersList;</span><br><span class="line">&#x2F;&#x2F;订单orders表</span><br><span class="line">    private Integer id;</span><br><span class="line">    private String orderNumber;</span><br><span class="line">    private Double orderPrice;</span><br></pre></td></tr></table></figure>

<p>因为返回的数据不仅仅有客户表中的信息，还有订单表中的信息，所以数据库要多表连接查询，查询的数据的不同，所以要使用resultMap进行一个绑定</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;resultMap id&#x3D;&quot;customermap&quot; type&#x3D;&quot;customer&quot;&gt;</span><br><span class="line">    &lt;id property&#x3D;&quot;id&quot; column&#x3D;&quot;cid&quot;&gt;&lt;&#x2F;id&gt;</span><br><span class="line">    &lt;result property&#x3D;&quot;name&quot; column&#x3D;&quot;name&quot;&gt;&lt;&#x2F;result&gt;</span><br><span class="line">    &lt;result property&#x3D;&quot;age&quot; column&#x3D;&quot;age&quot;&gt;&lt;&#x2F;result&gt;</span><br><span class="line">    &lt;!--orders属性绑定--&gt;</span><br><span class="line">    &lt;collection property&#x3D;&quot;ordersList&quot; ofType&#x3D;&quot;orders&quot;&gt;</span><br><span class="line">        &lt;id property&#x3D;&quot;id&quot; column&#x3D;&quot;oid&quot;&gt;&lt;&#x2F;id&gt;</span><br><span class="line">        &lt;result property&#x3D;&quot;orderNumber&quot; column&#x3D;&quot;orderNumber&quot;&gt;&lt;&#x2F;result&gt;</span><br><span class="line">        &lt;result property&#x3D;&quot;orderPrice&quot; column&#x3D;&quot;orderPrice&quot;&gt;&lt;&#x2F;result&gt;</span><br><span class="line">    &lt;&#x2F;collection&gt;</span><br><span class="line">&lt;&#x2F;resultMap&gt;</span><br><span class="line"></span><br><span class="line">&lt;select id&#x3D;&quot;getById&quot; parameterType&#x3D;&quot;int&quot; resultMap&#x3D;&quot;customermap&quot;&gt;</span><br><span class="line">    select c.id cid,name,age,o.id oid,orderNumber,orderPrice,customer_id</span><br><span class="line">    from customer c left join orders o on c.id &#x3D; o.customer_id</span><br><span class="line">    where c.id &#x3D; #&#123;id&#125;</span><br><span class="line">&lt;&#x2F;select&gt;</span><br></pre></td></tr></table></figure>

<p>多对一关联关系：订单和客户就是多对一关联，站在订单的方向查询同时将客户信息查询出来这就是多对一查询，同理可以在订单的实体类中设置用户customer为属性，然后创建订单的接口和其Mapper.xml文件</p>
<p>总结：无论什么关联关系，如果某一方持有另一方的集合，则使用标签完成映射，如果某方持有另一方对象，则使用标签完成映射</p>
<h2 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h2><p>多个操作同时完成，或者同时失败成为事务处理</p>
<ul>
<li>事务有四个特性：一致性，持久性，原子性，隔离性</li>
<li>在mybatis中是设置事务,设置为程序员自己处理的提交和回滚</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;transactionManager type&#x3D;&quot;JDBC&quot;&gt;&lt;&#x2F;transactionManager&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>也就可以设置为自动提交</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sqlSession &#x3D; factory.openSession(); &#x2F;&#x2F;默认是手工提交事务，设置为false也是手动提交事务，设置为true为自动提交事务</span><br><span class="line">sqlSession &#x3D; factory.openSession(true);&#x2F;&#x2F;设置为自动提交，在增删改后面不需要commit()</span><br></pre></td></tr></table></figure>

<h2 id="缓存"><a href="#缓存" class="headerlink" title="缓存"></a>缓存</h2><ul>
<li>mybatis框架提供了两级缓存，一级缓存和二级缓存，默认开启一级缓存</li>
<li>缓存就是为了提高查询效率，如果数据库中发生改变，那么将会清空缓存</li>
<li>使用缓存后查询的流程：</li>
<li>查询时先到缓存里面查，如果没有则去数据库查询，放在缓存一份，然后返回给客户端，下次再查询的时候，不再访问数据库，直接在缓存里面拿。如果数据库发生了commit()操作，那么将会清空缓存。</li>
<li>一级缓存，使用的是sqlSession的作用域，同一个sqlSession共享一级缓存的数据</li>
<li>二级缓存使用的是mapper的作用域，不同的sqlSession只要访问的是同一个mapper.xml文件，则共享二级缓存作用域</li>
</ul>
<h2 id="ORM"><a href="#ORM" class="headerlink" title="ORM"></a>ORM</h2><ul>
<li>ORM(Object Relational Mapping):对象映射关系</li>
<li>mybatis框架是ORM非常优秀的框架</li>
<li>Java语言中以对象的方式操作数据，存到数据库中是以表的方式进行存储，对象中的成员变量与表中的列之间的数据互换成为映射，整个这套操作就是ORM</li>
<li>持久化的操作：将对象保存到关系型数据库中，将关系型数据库中的数据读出来以对象的形式封装</li>
<li>mybatis是持久化层优秀的框架</li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="老苏叶 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="老苏叶 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Spring/" rel="tag"><i class="fa fa-tag"></i> Spring</a>
          
            <a href="/tags/Java-SSM/" rel="tag"><i class="fa fa-tag"></i> Java - SSM</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2024/06/20/netkeeper%E6%A0%A1%E5%9B%AD%E7%BD%91%E7%A0%B4%E8%A7%A3/" rel="next" title="netkeeper校园网破解">
                <i class="fa fa-chevron-left"></i> netkeeper校园网破解
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2024/09/11/%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E8%87%AA%E5%B7%B1%E7%9A%84%E5%9B%BE%E5%BA%8A%E7%A8%8B%E5%BA%8F/" rel="prev" title="实现一个自己的图床程序">
                实现一个自己的图床程序 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatra.jpg"
                alt="老苏叶" />
            
              <p class="site-author-name" itemprop="name">老苏叶</p>
              <p class="site-description motion-element" itemprop="description">狂热的代码爱好者卑微的菜鸟程序员</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">35</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/rss2.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/By-laosuye/By-laosuye.github.io.git" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://39.106.175.113" target="_blank" title="空间">
                      
                        <i class="fa fa-fw fa-vk"></i>空间</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="/qq" target="_blank" title="QQ">
                      
                        <i class="fa fa-fw fa-qq"></i>QQ</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="/weixin" target="_blank" title="微信">
                      
                        <i class="fa fa-fw fa-weixin"></i>微信</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#三层架构"><span class="nav-text">三层架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SSM框架"><span class="nav-text">SSM框架</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是MyBatis框架"><span class="nav-text">什么是MyBatis框架</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Mybatis对象分析"><span class="nav-text">Mybatis对象分析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、Resources类"><span class="nav-text">1、Resources类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、SqlSessionFactoryBuilder接口"><span class="nav-text">2、SqlSessionFactoryBuilder接口</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#为实体类注册别名"><span class="nav-text">为实体类注册别名</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、单个注册"><span class="nav-text">1、单个注册</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、批量注册"><span class="nav-text">2、批量注册</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#动态代理"><span class="nav-text">动态代理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、动态代理存在的意义"><span class="nav-text">1、动态代理存在的意义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、动态代理的实现规范"><span class="nav-text">2、动态代理的实现规范</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、动态代理的访问"><span class="nav-text">3、动态代理的访问</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#和-的区别"><span class="nav-text">#{}和${}的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#返回主键业务"><span class="nav-text">返回主键业务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#动态SQL"><span class="nav-text">动态SQL</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#指定参数位置"><span class="nav-text">指定参数位置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#入参为Map"><span class="nav-text">入参为Map</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#返回值是map"><span class="nav-text">返回值是map</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#resultMap的用法"><span class="nav-text">resultMap的用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#表的关联关系"><span class="nav-text">表的关联关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事务"><span class="nav-text">事务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#缓存"><span class="nav-text">缓存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ORM"><span class="nav-text">ORM</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
       <div>
       <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=30431643&auto=1&height=66"></iframe>
       </div>

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">老苏叶</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">本站总字数&#58;</span>
    
    <span title="本站总字数">52.6k</span>
  
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i> 访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i> 访问总量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: true,
        appId: '4ePJvrPIyoMuyL54g1K6fNGc-gzGzoHsz',
        appKey: 'MKyiWYqHi0PIzxhH8jC1Q9pe',
        placeholder: '留下你的爪印吧～',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  
  

  

  
  

  

  

  

</body>
</html>
